package com.kitty.game.statistics.facade;

import com.kitty.game.role.model.Role;
import com.kitty.game.statistics.service.StatsService;
import com.kitty.game.utils.TimeUtil;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//后加
import com.kitty.listener.event.DeadPunishEvent;
import com.kitty.listener.event.MonthTaoChangeEvent;
import com.kitty.listener.event.PotChangeEvent;
import com.kitty.listener.event.RoleExpChangeEvent;
import com.kitty.listener.event.TaoChangeEvent;

@Controller
public class StatsController {
    @Autowired
    StatsService statsService;

    /**人物经验改变时改变今日统计*/
    @EventHandler(EventType.ROLE_EXP_CHANGE)
    public void handleRoleExpChangeEvent(RoleExpChangeEvent roleExpChangeEvent) {
        Role role = roleExpChangeEvent.getRole();
        statsService.updateDailyRoleExpStats(role, roleExpChangeEvent.getChange());
        if (role.getOfflineNotice().getLastTime() >0){
            long leiji = role.getOfflineNotice().getLeijiTime();
            long cost = System.currentTimeMillis()-role.getOfflineNotice().getLastTime() - 2* TimeUtil.ONE_MINUTE;
            role.getOfflineNotice().setLeijiTime(leiji + (cost>0?cost:0));
        }
        role.getOfflineNotice().setLastTime(System.currentTimeMillis());
    }

    /**道行改变时改变今日统计*/
    @EventHandler(EventType.TAO_CHANGE)
    public void handleTaoChangeEvent(TaoChangeEvent taoChangeEvent) {
        Role role = taoChangeEvent.getRole();
        statsService.updateDailyTaoStats(role, taoChangeEvent.getChange());
        if (role.getOfflineNotice().getLastTime() >0){
            long leiji = role.getOfflineNotice().getLeijiTime();
            long cost = System.currentTimeMillis()-role.getOfflineNotice().getLastTime() - 2* TimeUtil.ONE_MINUTE;
            role.getOfflineNotice().setLeijiTime(leiji + (cost>0?cost:0));
        }
        role.getOfflineNotice().setLastTime(System.currentTimeMillis());
    }

    /**月道行改变时改变今日统计*/
    @EventHandler(EventType.MONTH_TAO_CHANGE)
    public void handleMonthTaoChangeEvent(MonthTaoChangeEvent monthTaoChangeEvent) {
        Role role = monthTaoChangeEvent.getRole();
        statsService.updateDailyMonthTaoStats(role, monthTaoChangeEvent.getChange());
    }

    /**潜能改变时改变今日统计*/
    @EventHandler(EventType.POT_CHANGE)
    public void handlePotChangeEvent(PotChangeEvent potChangeEvent) {
        Role role = potChangeEvent.getRole();
        statsService.updateDailyPotStats(role, potChangeEvent.getChange());
    }

    /**死亡惩罚时改变今日统计*/
    @EventHandler(EventType.DEAD_PUNISH)
    public void handleDeadPunishEvent(DeadPunishEvent deadPunishEvent) {
        Role role = deadPunishEvent.getRole();
        statsService.addDailyDeadStats(role, 1);
    }
}
